/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2006，所有权利保留。
 * 
 * 文件名：	ViewRoleSystem.java
 * 模块说明：	
 * 修改历史：
 * 2006-8-30 - lxm - 创建。
 */
package com.hd123.rumba.action.user;

import net.sf.hibernate.HibernateException;

import com.hd123.rumba.cartridge.FVRumbaSystem;
import com.hd123.rumba.domain.biz.BDomain;
import com.hd123.rumba.domain.biz.IBDomainMgr;
import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.request.ActionConstants;
import com.hd123.rumba.runtime.NamingMgr;
import com.hd123.rumba.sql.SCE;
import com.hd123.rumba.sql.biz.BSQLSubquery;
import com.hd123.rumba.user.CPermission;
import com.hd123.rumba.user.CRole;
import com.hd123.rumba.user.PE;

/**
 * 角色 | Action | FVRumbaSystem
 * 
 * @author lxm
 * 
 */
public class ViewRoleSystem extends BaseViewRole {

  public static final String CAPTION_VIEW = "角色维护";

  /* 权限常量 */
  public static final String FUNCV_PREFIX = FVRumbaSystem.FUNCV_PREFIX;
  public static final String PERMISSION_CREATE = FUNCV_PREFIX + ".角色.新建权";
  public static final String PERMISSION_VIEW = FUNCV_PREFIX + ".角色.查看权";
  public static final String PERMISSION_EDIT = FUNCV_PREFIX + ".角色.编辑权";
  public static final String PERMISSION_DELETE = FUNCV_PREFIX + ".角色.删除权";
  public static final String PERMISSION_SHARE = FUNCV_PREFIX + ".角色.共享公共角色权";
  public static final String PERMISSION_RECYCLE_VIEW = FUNCV_PREFIX + ".角色.回收站.查看权";
  public static final String PERMISSION_RECYCLE_EDIT = FUNCV_PREFIX + ".角色.回收站.编辑权";
  public static final String PERMISSION_RECYCLE_UNDELETE = FUNCV_PREFIX + ".角色.回收站.恢复删除权";
  public static final String PERMISSION_LOWER_CREATE = FUNCV_PREFIX + ".下级组织角色.新建权";
  public static final String PERMISSION_LOWER_VIEW = FUNCV_PREFIX + ".下级组织角色.查看权";
  public static final String PERMISSION_LOWER_EDIT = FUNCV_PREFIX + ".下级组织角色.编辑权";
  public static final String PERMISSION_LOWER_DELETE = FUNCV_PREFIX + ".下级组织角色.删除权";
  public static final String PERMISSION_LOWER_RECYCLE_VIEW = FUNCV_PREFIX + ".下级组织角色.回收站.查看权";
  public static final String PERMISSION_LOWER_RECYCLE_EDIT = FUNCV_PREFIX + ".下级组织角色.回收站.编辑权";
  public static final String PERMISSION_LOWER_RECYCLE_UNDELETE = FUNCV_PREFIX + ".下级组织角色.回收站.恢复删除权";

  @Override
  public String execute() throws Exception {
    String url = ActionConstants.NOTIMPLEMENTED;
    dd.put("viewName", "ViewRoleSystem");

    String startnode = (String) dd.getFormValue("start");
    if (startnode == null)
      startnode = "list";
    if ("list".equals(startnode))
      url = this.executeWithPermissions("list", PE.OR(new PE[] {
          PE.P(PERMISSION_VIEW), PE.P(PERMISSION_RECYCLE_VIEW), PE.P(PERMISSION_LOWER_VIEW),
          PE.P(PERMISSION_LOWER_RECYCLE_VIEW) }));
    else if ("filter".equals(startnode))
      url = this.executeWithPermission("filter", null);
    else if ("create".equals(startnode))
      url = this.executeWithPermissions("create", PE.OR(PE.P(PERMISSION_CREATE), PE
          .P(PERMISSION_LOWER_CREATE)));
    else if ("view".equals(startnode))
      url = this.executeWithPermissions("view", PE.OR(new PE[] {
          PE.P(PERMISSION_VIEW), PE.P(PERMISSION_RECYCLE_VIEW), PE.P(PERMISSION_LOWER_VIEW),
          PE.P(PERMISSION_LOWER_RECYCLE_VIEW) }));
    else if ("edit".equals(startnode))
      url = this.executeWithPermissions("edit", PE.OR(new PE[] {
          PE.P(PERMISSION_EDIT), PE.P(PERMISSION_RECYCLE_EDIT), PE.P(PERMISSION_LOWER_EDIT),
          PE.P(PERMISSION_LOWER_RECYCLE_EDIT) }));
    else if ("delete".equals(startnode))
      url = this.executeWithPermissions("delete", PE.OR(PE.P(PERMISSION_DELETE), PE
          .P(PERMISSION_LOWER_DELETE)));
    else if ("undelete".equals(startnode))
      url = this.executeWithPermissions("undelete", PE.OR(PE.P(PERMISSION_RECYCLE_UNDELETE), PE
          .P(PERMISSION_LOWER_RECYCLE_UNDELETE)));
    else if ("editRoleUsers".equals(startnode))
      url = this.executeWithPermissions("editRoleUsers", PE.OR(new PE[] {
          PE.P(PERMISSION_EDIT), PE.P(PERMISSION_RECYCLE_EDIT), PE.P(PERMISSION_LOWER_EDIT),
          PE.P(PERMISSION_LOWER_RECYCLE_EDIT),
          PE.P(CRole.PERMISSION_FA_USERS + CPermission.SUFFIX_PERMISSION_EDIT) }));
    else if ("saveRoleUsers".equals(startnode))
      url = this.executeWithPermissions("saveRoleUsers", PE.OR(new PE[] {
          PE.P(PERMISSION_EDIT), PE.P(PERMISSION_RECYCLE_EDIT), PE.P(PERMISSION_LOWER_EDIT),
          PE.P(PERMISSION_LOWER_RECYCLE_EDIT),
          PE.P(CRole.PERMISSION_FA_USERS + CPermission.SUFFIX_PERMISSION_EDIT) }));
    else if ("editRolePermissions".equals(startnode))
      url = this.executeWithPermissions("editRolePermissions", PE.OR(new PE[] {
          PE.P(PERMISSION_EDIT), PE.P(PERMISSION_RECYCLE_EDIT), PE.P(PERMISSION_LOWER_EDIT),
          PE.P(PERMISSION_LOWER_RECYCLE_EDIT) }));
    else if ("saveRolePermissions".equals(startnode))
      url = this.executeWithPermissions("saveRolePermissions", PE.OR(new PE[] {
          PE.P(PERMISSION_EDIT), PE.P(PERMISSION_RECYCLE_EDIT), PE.P(PERMISSION_LOWER_EDIT),
          PE.P(PERMISSION_LOWER_RECYCLE_EDIT) }));
    else if ("editRoleShare".equals(startnode))
      url = this.executeWithPermission("editRoleShare", PERMISSION_SHARE);
    else if ("saveRoleShare".equals(startnode))
      url = this.executeWithPermission("saveRoleShare", PERMISSION_SHARE);
    else if ("browse".equals(startnode))
      url = this.executeWithPermission("browse", null);
    else if ("dispatch".equals(startnode)) {
      String action = (String) dd.getFormValue("action");
      if ("save".equals(action)) {
        String isRecycle = (String) dd.getFormValue("isRecycle");
        if ("true".equalsIgnoreCase(isRecycle)) {
          String uuid = (String) dd.getFormValue("role_uuid");
          if ("".equals(uuid))
            uuid = null;
          if (uuid == null)
            url = this.executeWithPermissions("save", PE.OR(PE.P(PERMISSION_CREATE), PE
                .P(PERMISSION_LOWER_CREATE)));
          else
            url = this.executeWithPermissions("save", PE.OR(PE.P(PERMISSION_EDIT), PE
                .P(PERMISSION_LOWER_EDIT)));
        } else
          url = this.executeWithPermissions("save", PE.OR(PE.P(PERMISSION_RECYCLE_EDIT), PE
              .P(PERMISSION_LOWER_RECYCLE_EDIT)));
      } else if ("deleteSelected".equals(action))
        url = this.executeWithPermissions("deleteSelected", PE.OR(PE.P(PERMISSION_DELETE), PE
            .P(PERMISSION_LOWER_DELETE)));
      else if ("undeleteSelected".equals(action))
        url = this.executeWithPermissions("undeleteSelected", PE.OR(PE
            .P(PERMISSION_RECYCLE_UNDELETE), PE.P(PERMISSION_LOWER_RECYCLE_UNDELETE)));
    }

    return url;
  }

  public String getPermission(String functionName, String domainUuid, Boolean isRecycle)
      throws BusinessException, HibernateException {
    IBDomainMgr domainMgr = (IBDomainMgr) NamingMgr.getInstance().lookupMgr(IBDomainMgr.class);
    BDomain bdomain = domainMgr.loadDomain(domainUuid);
    int relationship = domainMgr.getRelativePosition(getCurrentDomain(), bdomain);
    if (relationship == IBDomainMgr.VALUE_RELATIONSHIP_SELF) {
      if (!isRecycle.booleanValue()) {
        if (ViewRole.FUNCTION_CREATE.equals(functionName)) {
          return PERMISSION_CREATE;
        } else if (ViewRole.FUNCTION_EDIT.equals(functionName)) {
          return PERMISSION_EDIT;
        } else if (ViewRole.FUNCTION_VIEW.equals(functionName)) {
          return PERMISSION_VIEW;
        } else if (ViewRole.FUNCTION_DELETE.equals(functionName)) {
          return PERMISSION_DELETE;
        } else if (ViewRole.FUNCTION_SHARE.equals(functionName))
          return PERMISSION_SHARE;
      } else {
        if (ViewRole.FUNCTION_EDIT.equals(functionName)) {
          return PERMISSION_RECYCLE_EDIT;
        } else if (ViewRole.FUNCTION_VIEW.equals(functionName)) {
          return PERMISSION_RECYCLE_VIEW;
        } else if (ViewRole.FUNCTION_UNDELETE.equals(functionName)) {
          return PERMISSION_RECYCLE_UNDELETE;
        }
      }
    } else if (relationship == IBDomainMgr.VALUE_RELATIONSHIP_LOWER) {
      if (!isRecycle.booleanValue()) {
        if (ViewRole.FUNCTION_CREATE.equals(functionName)) {
          return PERMISSION_LOWER_CREATE;
        } else if (ViewRole.FUNCTION_EDIT.equals(functionName)) {
          return PERMISSION_LOWER_EDIT;
        } else if (ViewRole.FUNCTION_VIEW.equals(functionName)) {
          return PERMISSION_LOWER_VIEW;
        } else if (ViewRole.FUNCTION_DELETE.equals(functionName)) {
          return PERMISSION_LOWER_DELETE;
        }
      } else {
        if (ViewRole.FUNCTION_EDIT.equals(functionName)) {
          return PERMISSION_LOWER_RECYCLE_EDIT;
        } else if (ViewRole.FUNCTION_VIEW.equals(functionName)) {
          return PERMISSION_LOWER_RECYCLE_VIEW;
        } else if (ViewRole.FUNCTION_UNDELETE.equals(functionName)) {
          return PERMISSION_LOWER_RECYCLE_UNDELETE;
        }
      }
    } else if (relationship == IBDomainMgr.VALUE_RELATIONSHIP_UPPER) {
      if (!isRecycle.booleanValue()) {
        if (ViewRole.FUNCTION_VIEW.equals(functionName))
          return PERMISSION_VIEW;
      } else {
        if (ViewRole.FUNCTION_VIEW.equals(functionName))
          return PERMISSION_RECYCLE_VIEW;
      }
    }
    return null;
  }

  protected void addForceFilter(BSQLSubquery bsq) throws BusinessException, HibernateException {
    SCE sce = new SCE(SCE.VALUE_OPERATOR_AND);
    String levelId = getCurrentDomain().getLevelId();
    sce.append(SCE.cond("r.domain.levelId like ?", levelId, SCE.WC_RIGHT));

    SCE sce2 = new SCE(SCE.VALUE_OPERATOR_OR);

    PE pe = PE.OR(PE.P(PERMISSION_VIEW), PE.P(PERMISSION_RECYCLE_VIEW));
    if (getCurrentUser().hasPermissions(pe)) {
      SCE and = new SCE(SCE.VALUE_OPERATOR_AND);
      and.append(SCE.cond("r.domain.uuid = ?", getCurrentDomain().getUuid()));
      SCE or = new SCE(SCE.VALUE_OPERATOR_OR);
      if (getCurrentUser().hasPermission(PERMISSION_VIEW))
        or.append(SCE.cond("r.role.state != ?", CRole.VALUE_STATE_DELETED));
      if (getCurrentUser().hasPermission(PERMISSION_RECYCLE_VIEW))
        or.append(SCE.cond("r.role.state = ?", CRole.VALUE_STATE_DELETED));
      and.append(or);
      sce2.append(and);
    }

    pe = PE.OR(PE.P(PERMISSION_LOWER_VIEW), PE.P(PERMISSION_LOWER_RECYCLE_VIEW));
    if (getCurrentUser().hasPermissions(pe)) {
      SCE and = new SCE(SCE.VALUE_OPERATOR_AND);
      and.append(SCE.cond("r.domain.uuid != ?", getCurrentDomain().getUuid()));
      SCE or = new SCE(SCE.VALUE_OPERATOR_OR);
      if (getCurrentUser().hasPermission(PERMISSION_LOWER_VIEW))
        or.append(SCE.cond("r.role.state != ?", CRole.VALUE_STATE_DELETED));
      if (getCurrentUser().hasPermission(PERMISSION_LOWER_RECYCLE_VIEW))
        or.append(SCE.cond("r.role.state = ?", CRole.VALUE_STATE_DELETED));
      and.append(or);
      sce2.append(and);
    }

    sce.append(sce2);
    bsq.setWhereCondition(SCE.and(bsq.getWhereCondition(), sce));
  }
}
